package com.wgh.product.controller;

import com.wgh.common.vo.Result;
import com.wgh.product.dto.ProductSkuAttributeDTO;
import com.wgh.product.entity.shop.ProductSkuAttribute;
import com.wgh.product.service.ProductSkuAttributeService;
import com.wgh.product.converter.ProductSkuAttributeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * SKU属性关联控制器
 * 提供SKU属性关联相关的RESTful API
 */
@RestController
@RequestMapping("/api/product-sku-attribute")
public class ProductSkuAttributeController {

    @Autowired
    private ProductSkuAttributeService productSkuAttributeService;

    @Autowired
    private ProductSkuAttributeConverter productSkuAttributeConverter;

    /**
     * 根据SKU ID查询属性关联列表
     */
    @GetMapping("/sku/{skuId}")
    public Result<List<ProductSkuAttributeDTO>> getSkuAttributesBySkuId(@PathVariable Long skuId) {
        try {
            List<ProductSkuAttribute> skuAttributes = productSkuAttributeService.getSkuAttributesBySkuId(skuId);
            return Result.success(productSkuAttributeConverter.toDTOList(skuAttributes));
        } catch (Exception e) {
            return Result.error("获取SKU属性关联列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据SKU ID列表查询属性关联列表
     */
    @GetMapping("/sku/batch")
    public Result<List<ProductSkuAttributeDTO>> getSkuAttributesBySkuIds(@RequestParam List<Long> skuIds) {
        try {
            List<ProductSkuAttribute> skuAttributes = productSkuAttributeService.getSkuAttributesBySkuIds(skuIds);
            return Result.success(productSkuAttributeConverter.toDTOList(skuAttributes));
        } catch (Exception e) {
            return Result.error("批量获取SKU属性关联列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据属性ID查询SKU属性关联
     */
    @GetMapping("/attribute/{attributeId}")
    public Result<List<ProductSkuAttributeDTO>> getSkuAttributesByAttributeId(@PathVariable Long attributeId) {
        try {
            List<ProductSkuAttribute> skuAttributes = productSkuAttributeService.getSkuAttributesByAttributeId(attributeId);
            return Result.success(productSkuAttributeConverter.toDTOList(skuAttributes));
        } catch (Exception e) {
            return Result.error("根据属性ID获取SKU属性关联失败：" + e.getMessage());
        }
    }

    /**
     * 根据属性值ID查询SKU属性关联
     */
    @GetMapping("/attribute-value/{attributeValueId}")
    public Result<List<ProductSkuAttributeDTO>> getSkuAttributesByAttributeValueId(@PathVariable Long attributeValueId) {
        try {
            List<ProductSkuAttribute> skuAttributes = productSkuAttributeService.getSkuAttributesByAttributeValueId(attributeValueId);
            return Result.success(productSkuAttributeConverter.toDTOList(skuAttributes));
        } catch (Exception e) {
            return Result.error("根据属性值ID获取SKU属性关联失败：" + e.getMessage());
        }
    }

    /**
     * 批量新增SKU属性关联
     */
    @PostMapping("/batch")
    public Result<Boolean> batchAddSkuAttributes(@RequestBody List<ProductSkuAttributeDTO> skuAttributeDTOs) {
        try {
            List<ProductSkuAttribute> skuAttributes = productSkuAttributeConverter.toEntityList(skuAttributeDTOs);
            boolean result = productSkuAttributeService.batchAddSkuAttributes(skuAttributes);
            return result ? Result.success(true) : Result.error("批量新增SKU属性关联失败");
        } catch (Exception e) {
            return Result.error("批量新增SKU属性关联失败：" + e.getMessage());
        }
    }

    /**
     * 根据SKU ID删除属性关联
     */
    @DeleteMapping("/sku/{skuId}")
    public Result<Boolean> deleteSkuAttributesBySkuId(@PathVariable Long skuId) {
        try {
            boolean result = productSkuAttributeService.deleteSkuAttributesBySkuId(skuId);
            return result ? Result.success(true) : Result.error("删除SKU属性关联失败");
        } catch (Exception e) {
            return Result.error("删除SKU属性关联失败：" + e.getMessage());
        }
    }

    /**
     * 根据SKU ID列表批量删除属性关联
     */
    @DeleteMapping("/sku/batch")
    public Result<Boolean> batchDeleteSkuAttributesBySkuIds(@RequestParam List<Long> skuIds) {
        try {
            boolean result = productSkuAttributeService.batchDeleteSkuAttributesBySkuIds(skuIds);
            return result ? Result.success(true) : Result.error("批量删除SKU属性关联失败");
        } catch (Exception e) {
            return Result.error("批量删除SKU属性关联失败：" + e.getMessage());
        }
    }

    /**
     * 根据属性ID删除SKU属性关联
     */
    @DeleteMapping("/attribute/{attributeId}")
    public Result<Boolean> deleteSkuAttributesByAttributeId(@PathVariable Long attributeId) {
        try {
            boolean result = productSkuAttributeService.deleteSkuAttributesByAttributeId(attributeId);
            return result ? Result.success(true) : Result.error("根据属性ID删除SKU属性关联失败");
        } catch (Exception e) {
            return Result.error("根据属性ID删除SKU属性关联失败：" + e.getMessage());
        }
    }

    /**
     * 根据商品ID查询SKU属性关联
     */
    @GetMapping("/product/{productId}")
    public Result<List<ProductSkuAttributeDTO>> getSkuAttributesByProductId(@PathVariable Long productId) {
        try {
            List<ProductSkuAttribute> skuAttributes = productSkuAttributeService.getSkuAttributesByProductId(productId);
            return Result.success(productSkuAttributeConverter.toDTOList(skuAttributes));
        } catch (Exception e) {
            return Result.error("根据商品ID获取SKU属性关联失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取SKU属性关联详情
     */
    @GetMapping("/{id}")
    public Result<ProductSkuAttributeDTO> getSkuAttributeById(@PathVariable Long id) {
        try {
            ProductSkuAttribute skuAttribute = productSkuAttributeService.getById(id);
            if (skuAttribute == null) {
                return Result.success(null);
            }
            return Result.success(productSkuAttributeConverter.toDTO(skuAttribute));
        } catch (Exception e) {
            return Result.error("获取SKU属性关联详情失败：" + e.getMessage());
        }
    }

    /**
     * 批量查询SKU属性关联
     */
    @GetMapping("/batch")
    public Result<List<ProductSkuAttributeDTO>> getSkuAttributesByIds(@RequestParam List<Long> ids) {
        try {
            List<ProductSkuAttribute> skuAttributes = productSkuAttributeService.listByIds(ids);
            return Result.success(productSkuAttributeConverter.toDTOList(skuAttributes));
        } catch (Exception e) {
            return Result.error("批量获取SKU属性关联失败：" + e.getMessage());
        }
    }
}